package game.multimedia;

import javax.sound.midi.MidiSystem;
import javax.sound.midi.Sequence;
import javax.sound.midi.Sequencer;

import com.badlogic.gdx.files.FileHandle;

public class IMusicMidi implements IMusic {

	private Sequence sequence;
	private Sequencer sequencer;
	
	
	@Override
	public void FromFile(FileHandle file) {
		try {
			sequence = MidiSystem.getSequence(file.read());
			if (sequencer == null) {
				sequencer = MidiSystem.getSequencer();
			}
			sequencer.open();
			sequencer.setSequence(sequence);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean IsLooping() {
		if (sequencer != null) {
			return sequencer.getLoopCount() != 0;
		}
		return false;
	}

	@Override
	public void SetLooping(boolean loop) {
		if (sequencer != null) {
			if (!loop) {
				sequencer.setLoopCount(0);
				return;
			}
			sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY);
		}
	}

	@Override
	public void Play() {
		if (sequencer != null) {
			sequencer.start();
		}
	}

	@Override
	public void Pause() {
		Stop();
	}

	@Override
	public void Resume() {
		Play();

	}

	@Override
	public void Stop() {
		if (sequencer != null) {
			sequencer.stop();
		}
	}

	@Override
	public void Release() {
		if (sequencer != null) {
			sequencer.close();
		}
	}

}
